package routers

import (
	"net/http"

	"github.com/gorilla/mux"

	"eccgateway/pkg/controllers"
	"eccgateway/pkg/controllers/dashboard"
	"eccgateway/pkg/controllers/kubernetes"
)

func RegisterRouter() http.Handler {
	r := mux.NewRouter()

	r.HandleFunc("/v1/auth/tokens", controllers.AuthController{}.ServeHTTP).Methods(http.MethodPost)
	r.HandleFunc("/v1/auth/tokens", controllers.AuthController{}.ServeHTTP).Methods(http.MethodGet)
	r.HandleFunc("/camera/models", controllers.GetDeploymentNamesByCameraModelName).Methods(http.MethodGet)
	r.HandleFunc("/camera/models", controllers.DeleteDeploymentAndServiceByCameraModelName).Methods(http.MethodDelete)

	r.HandleFunc("/device/reading", controllers.GetDeviceValueHandler).Methods(http.MethodPost)
	r.HandleFunc("/v1/image", controllers.ImageController{}.UploadImage).Methods(http.MethodPost)

	r.HandleFunc("/v1/batch", controllers.GetBatchController().Create).Methods(http.MethodPost)
	r.HandleFunc("/v1/batch", controllers.GetBatchController().List).Methods(http.MethodGet)
	r.HandleFunc("/v1/batch/{name}", controllers.GetBatchController().Get).Methods(http.MethodGet)
	r.HandleFunc("/v1/batch/{name}/stop", controllers.GetBatchController().Stop).Methods(http.MethodPut)
	r.HandleFunc("/v1/batch/{name}/start", controllers.GetBatchController().Start).Methods(http.MethodPut)
	r.HandleFunc("/v1/batch/{name}", controllers.GetBatchController().Delete).Methods(http.MethodDelete)

	r.HandleFunc("/v1/appTempl", controllers.GetAppTemplController().TemplCreate).Methods(http.MethodPost, http.MethodPut)
	r.HandleFunc("/v1/appTempl", controllers.GetAppTemplController().TempList).Methods(http.MethodGet)
	r.HandleFunc("/v1/appTempl/pubTemplList", controllers.GetAppTemplController().PubTemplList).Methods(http.MethodGet)
	r.HandleFunc("/v1/appTempl/templ/{uuid}", controllers.GetAppTemplController().GetTempl).Methods(http.MethodGet)
	r.HandleFunc("/v1/appTempl/{uuid}", controllers.GetAppTemplController().TemplDelete).Methods(http.MethodDelete)
	r.HandleFunc("/v1/appTempl/public/{uuid}", controllers.GetAppTemplController().PublicHandler).Methods(http.MethodPut)
	r.HandleFunc("/v1/appTempl/audit", controllers.GetAppTemplController().AuditHandler).Methods(http.MethodPut)
	r.HandleFunc("/v1/appTempl/{templName}/{version}/logs", controllers.GetAppTemplController().GetTemplLogs).Methods(http.MethodGet)
	r.HandleFunc("/v1/appTempl/private/{uuid}", controllers.GetAppTemplController().PrivateHandler).Methods(http.MethodPut)
	r.HandleFunc("/v1/appTempl/download/{uuid}", controllers.GetAppTemplController().DownLoadHandler).Methods(http.MethodPut)
	r.HandleFunc("/v1/appTempl/increaseStar/{uuid}", controllers.GetAppTemplController().IncreaseStarHandler).Methods(http.MethodPut)
	r.HandleFunc("/v1/appTempl/decreaseStar/{uuid}", controllers.GetAppTemplController().DecreaseStarHandler).Methods(http.MethodPut)
	r.HandleFunc("/v1/appTempl/starTemplList", controllers.GetAppTemplController().GetStarTempls).Methods(http.MethodGet)

	r.HandleFunc("/v1/node", dashboard.DashboardController{}.GetNodesBySelfFilter).Methods(http.MethodGet)
	r.HandleFunc("/v1/node/register", controllers.NodeRegisterController{}.Create).Methods(http.MethodPost)

	addKubernetesHandle(r)

	addProxyHandle(r)
	return r
}

func addProxyHandle(r *mux.Router) {
	r.Handle(`/edgex/{rest:.+}`, &controllers.EdgeXController{})
	r.Handle(`/kubernetes/{rest:.+}`, &kubernetes.KubernetesController{})
	r.Handle(`/k8s-origin/{rest:.+}`, &kubernetes.KubernetesController{})
	r.Handle(`/dashboard/{rest:.+}`, &dashboard.DashboardController{})
}

func addKubernetesHandle(r *mux.Router) {
	r.HandleFunc(`/kubernetes/apis/endedgecloud.zjlab.io/v1/clusters`, kubernetes.ClusterController{}.ListCluster).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/endedgecloud.zjlab.io/v1/clusters`, kubernetes.ClusterController{}.CreateCluster).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/apis/endedgecloud.zjlab.io/v1/clusters/{name}`, kubernetes.ClusterController{}.GetCluster).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/endedgecloud.zjlab.io/v1/clusters/{name}`, kubernetes.ClusterController{}.UpdateCluster).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/apis/endedgecloud.zjlab.io/v1/clusters/{name}`, kubernetes.ClusterController{}.DeleteCluster).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/api/v1/nodes`, kubernetes.NodeController{}.ListNode).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/nodes/{name}`, kubernetes.NodeController{}.GetNode).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/nodes/{name}`, kubernetes.NodeController{}.UpdateNode).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/api/v1/nodes/{name}`, kubernetes.NodeController{}.DeleteNode).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/api/v1/namespaces`, kubernetes.NamespaceController{}.ListNamespace).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/namespaces`, kubernetes.NamespaceController{}.CreateNamespace).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{name}`, kubernetes.NamespaceController{}.GetNamespace).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{name}`, kubernetes.NamespaceController{}.UpdateNamespace).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{name}`, kubernetes.NamespaceController{}.DeleteNamespace).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/apis/apps/v1/namespaces/{namespace}/deployments`, kubernetes.DeployController{}.ListDeploy).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/apps/v1/namespaces/{namespace}/deployments`, kubernetes.DeployController{}.CreateDeploy).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/apis/apps/v1/namespaces/{namespace}/deployments/{name}`, kubernetes.DeployController{}.GetDeploy).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/apps/v1/namespaces/{namespace}/deployments/{name}`, kubernetes.DeployController{}.UpdateDeploy).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/apis/apps/v1/namespaces/{namespace}/deployments/{name}`, kubernetes.DeployController{}.DeleteDeploy).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/pods`, kubernetes.PodController{}.ListPod).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/pods`, kubernetes.PodController{}.CreatePod).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/pods/{name}`, kubernetes.PodController{}.GetPod).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/pods/{name}`, kubernetes.PodController{}.UpdatePod).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/pods/{name}`, kubernetes.PodController{}.DeletePod).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/services`, kubernetes.ServiceController{}.ListService).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/services`, kubernetes.ServiceController{}.CreateService).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/services/{name}`, kubernetes.ServiceController{}.GetService).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/services/{name}`, kubernetes.ServiceController{}.UpdateService).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/api/v1/namespaces/{namespace}/services/{name}`, kubernetes.ServiceController{}.DeleteService).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/apis/autoscaling/v1/namespaces/{namespace}/horizontalpodautoscalers`, kubernetes.HapControllerImpl.ListHpa).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/autoscaling/v1/namespaces/{namespace}/horizontalpodautoscalers`, kubernetes.HapControllerImpl.CreateHpa).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/apis/autoscaling/v1/namespaces/{namespace}/horizontalpodautoscalers/{name}`, kubernetes.HapControllerImpl.GetHpa).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/autoscaling/v1/namespaces/{namespace}/horizontalpodautoscalers/{name}`, kubernetes.HapControllerImpl.UpdateHpa).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/apis/autoscaling/v1/namespaces/{namespace}/horizontalpodautoscalers/{name}`, kubernetes.HapControllerImpl.DeleteHpa).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/ruleendpoints`, kubernetes.RuleEndpointControllerImpl.List).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/ruleendpoints`, kubernetes.RuleEndpointControllerImpl.Create).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/ruleendpoints/{name}`, kubernetes.RuleEndpointControllerImpl.Get).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/ruleendpoints/{name}`, kubernetes.RuleEndpointControllerImpl.Update).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/ruleendpoints/{name}`, kubernetes.RuleEndpointControllerImpl.Delete).Methods(http.MethodDelete)

	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/rules`, kubernetes.RuleControllerImpl.List).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/rules`, kubernetes.RuleControllerImpl.Create).Methods(http.MethodPost)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/rules/{name}`, kubernetes.RuleControllerImpl.Get).Methods(http.MethodGet)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/rules/{name}`, kubernetes.RuleControllerImpl.Update).Methods(http.MethodPatch)
	r.HandleFunc(`/kubernetes/apis/rules.kubeedge.io/v1/namespaces/{namespace}/rules/{name}`, kubernetes.RuleControllerImpl.Delete).Methods(http.MethodDelete)
}
